package com.example.recyclerviewtest;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.widget.TextView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private TextView titleText;

    private List<AppItem> appItemList = new ArrayList<>();  //子项列表
    private String[] appName = {"UC浏览器","抖音","啾咪星球","快手极速版","皮皮段子","全民省钱购","腾讯视频","腾讯新闻","西瓜视频","想看"};
    private int[] icons = {R.mipmap.uc_1,R.mipmap.dy_2,R.mipmap.jm_3,R.mipmap.ks_4,R.mipmap.dz_5,R.mipmap.sqg_6,R.mipmap.txsp_7,R.mipmap.txxw_8,R.mipmap.xg_9,R.mipmap.xk_10};

    private List<String> appRandomName = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.hide();
        }

        titleText = findViewById(R.id.title_text);
        titleText.setText("应用列表(RecyclerView Support)");


        setAppRandomLengthName();  //设置随机名字
        initApps();  //初始化应用数据
        RecyclerView recyclerView = findViewById(R.id.recycler_view);  //获取RecyclerView实例
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);  //创建线性布局管理器实例
//        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);  //设置横向布局
//        recyclerView.setLayoutManager(linearLayoutManager);  //设置线性布局

        //创建瀑布布局实例，纵向排列3列
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(
                3,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);  //设置瀑布布局

        AppAdapter appAdapter = new AppAdapter(appItemList);  //创建适配器
        recyclerView.setAdapter(appAdapter);  //设置适配器


    }


    /**
     * 为了瀑布布局效果，将appName中名字的长度随机改变
     */
    public void setAppRandomLengthName(){
        Random random = new Random();
        int length;
        for (String str :
                appName) {
            length = random.nextInt(20)+1;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < length; i++) {
                stringBuilder.append(str);
            }
            appRandomName.add(stringBuilder.toString());
        }
    }

    /**
     * 初始化子项列表数据
     */
    protected void initApps(){
        AppItem appItem;
        for (int i = 0; i < appName.length; i++) {
//            appItem = new AppItem(appName[i],icons[i]);
            appItem = new AppItem(appRandomName.get(i),icons[i]);
            appItemList.add(appItem);
        }
    }
}
